﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HPPClientLibrary;
using HPPClientUI.FileSystemTreeView;
using HPPUtil;
using HPPUtil.Helpers;

namespace HPPClientUI
{
    

    public partial class FrmSettings : MyFormBase
    {

        private string _selectedProperty;

        public ListView HashFileList
        {
            get
            {
                return this.lvHashFileList;
            }
        }

        public ListView ShareDirList
        {
            get
            {
                return this.lvShareDir;
            }
        }

        public ListView ConfigList
        {
            get
            {
                return this.lvConfig;
            }
        }

        private string SelectedProperty
        {
            get { return _selectedProperty; }
            set { _selectedProperty = value; }
        }

        public FrmSettings()
        {
            InitializeComponent();

        }

        public IEnumerable<String> ShareDirView
        {
            get { throw new NotSupportedException(); }
            set
            {
                lock (ShareDirList)
                {
                    SafeInvoke(() =>
                                   {
                                       this.ShareDirList.Items.Clear();
                                       this.ShareDirList.Items.AddRange(value.Cast(s => new ListViewItem(s)).ToArray());
                                   });
                    
                }

            }
        }

        public IEnumerable<KeyValuePair<string, HppFileInfo>> HashListView
        {
            private get { throw new NotSupportedException();}
            set
            {
                lock (HashFileList)
                {
                    SafeInvoke(() =>
                                   {
                                       this.HashFileList.Items.Clear();

                                       List<KeyValuePair<string, HppFileInfo>> list = new List<KeyValuePair<string, HppFileInfo>>(value);
                                       this.HashFileList.Items.AddRange(list.Cast(s => new ListViewItem(new string[]
                                                                                          {
                                                                                              s.Value.FullPathName,
                                                                                              s.Key,
                                                                                              
                                        })).ToArray());
                                   });
                    
                }
            }
        }

        public IEnumerable<KeyValuePair<string, string>> ConfigView
        {
            private get { throw new NotSupportedException(); }
            set
            {
                lock (ConfigList)
                {
                    SafeInvoke(() =>
                    {
                        this.ConfigList.Items.Clear();
                        this.ConfigList.Items.AddRange(value.Cast(s => new ListViewItem(new string[]
                                                                                          {
                                                                                              s.Key,
                                                                                              s.Value,
                                                                                              
                                                                                              
                                        })).ToArray());
                        
                    });

                }
            }
        }




        private void FrmSettings_Load(object sender, EventArgs e)
        {
            
            BindProxy.AddBind(this, StaticHelper.GetPropertyName<FrmSettings>(o => o.ShareDirView), FrmMain.Client, StaticHelper.GetPropertyName<HPPClient>(o => o.ReadOnlyShareDir), null);
            BindProxy.AddBind(this, StaticHelper.GetPropertyName<FrmSettings>(o => o.HashListView), FrmMain.Client, StaticHelper.GetPropertyName<HPPClient>(o => o.ReadOnlyHashFileInfoDict), null);

            BindProxy.AddBind(this, StaticHelper.GetPropertyName<FrmSettings>(o => o.ConfigView), FrmMain.Client.Config, StaticHelper.GetPropertyName<HPPClient>(o => o.Config.ValueList), null);
            
            this.ConfigView = FrmMain.Client.Config.ValueList;
            this.ShareDirView = FrmMain.Client.ReadOnlyShareDir;
            this.HashListView = FrmMain.Client.ReadOnlyHashFileInfoDict;
            
            this.fileSystemTreeView1.AddToSharedir += (send, eventArgs) => FrmMain.Client.AddShareDir(eventArgs.Path);
            
            this.fileSystemTreeView1.Load();
            

        }

 
        

        #region Button Click

        private void btnAdd_Click(object sender, EventArgs e)
        {
            DirectoryNode node = this.fileSystemTreeView1.SelectedNode as DirectoryNode;
            if (node == null)
            {
                return;
            }
            FrmMain.Client.AddShareDir(node.DirectoryInfo.FullName);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            FrmMain.Client.CheckNewShareDir();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        private void lvConfig_SelectedIndexChanged(object sender, EventArgs e)
        {
            var row = this.ConfigList.SelectedItems;

            if (row.Count != 0)
            {
                this.tbValue.Text = row[0].SubItems[1].Text;
                this.SelectedProperty = row[0].SubItems[0].Text;
            }
            
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            string value = this.tbValue.Text;
            
            FrmMain.Client.Config.SetProperty(SelectedProperty, value);
        }

    }
}
